//
//  KXTypeViewController.swift
//  KXBook
//
//  Created by  on 2022/9/10.
//

import UIKit

class KXTypeViewController: KXViewController {
    @IBOutlet var selectBtn: KXTypeSelectBtn!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var bgTableView: UIView!
    @IBOutlet var tableViewH: NSLayoutConstraint!
    var type : KXAddType = .pay
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LS("类别管理")
        self.loadArray(self.type)
        weak var weakself = self
        self.selectBtn.block = {type in
            weakself?.loadArray(type)
        }
        
        self.tableViewH.constant = CGFloat(0)
        self.tableView.register(KXMeCell.nib(), forCellReuseIdentifier: KXMeCell.ID())
        self.bgTableView.layer.masksToBounds = false
        self.bgTableView.layer.shadowColor = "#9B9B9B".color().cgColor
        self.bgTableView.layer.shadowOffset = CGSize(width:0, height:0)
        self.bgTableView.layer.shadowRadius = 10
        self.bgTableView.layer.shadowOpacity = 1
        self.bgTableView.layer.cornerRadius = 10
    }
    func loadArray(_ type:KXAddType){
        self.type = type
        let whereStr = String(format:"where %@=%@",bg_sqlKey("isPay"),bg_sqlValue("\(type == .pay ? 1 : 0)"))
        KXTypeModel.bg_findAsync(nil, where: whereStr, complete: {array in
            DispatchQueue.main.async {
                self.array = array as? [KXTypeModel] ?? [KXTypeModel]()
            }
        })
    }
    var array:[KXTypeModel] = [KXTypeModel](){
        didSet{
            self.tableView.reloadData()
            self.tableViewH.constant = CGFloat(65 * (array.count <= 6 ? array.count : 6))
            
        }
    }
    @IBAction func actionForBtn(_ sender: UIButton) {
        let vc = KXAddTypeViewController()
        vc.type = self.type
        self.push(vc, true)
    }
}
extension KXTypeViewController{
    override func addNotify() {
        NotifyTool.add(self, #selector(updateType), NotifyName.updateType)
    }
    
    @objc func updateType(){
        self.loadArray(self.type)
    }
}

//MARK: - UITableViewDataSource
extension KXTypeViewController : UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.array[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: KXMeCell.ID()) as! KXMeCell
        cell.title.text = LS(model.name)
        cell.img.isHidden = true
        cell.titleLeft.constant = -34
        return cell
    }
    
}
//MARK: - UITableViewDelegate
extension KXTypeViewController : UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = KXAddTypeViewController()
        vc.type = self.type
        vc.model = self.array[indexPath.row]
        self.push(vc, true)
    }
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .none
    }
    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        if #available(iOS 11.0, *){
            let edit = UIContextualAction(style:.normal, title:LS("编辑")) { action, view, handler in
                let vc = KXAddTypeViewController()
                vc.type = self.type
                vc.model = self.array[indexPath.row]
                self.push(vc, true)
            }
            edit.backgroundColor = "#E27BC1".color()
            let delete = UIContextualAction(style:.normal, title:LS("删除")) { action, view, handler in
                let model = self.array[indexPath.row]
                let whereStr = String(format:"where %@=%@",bg_sqlKey("name"),bg_sqlValue(model.name))
                KXTypeModel.bg_delete(nil, where:whereStr)
                self.array.remove(at: indexPath.row)
                tableView.reloadData()
            }
            delete.backgroundColor = "#ED593B".color()
            let config = UISwipeActionsConfiguration(actions: [delete,edit])
            config.performsFirstActionWithFullSwipe = false
            return config
        }
    }
}
